package com.nebula.maven.springbootmebula.beetl;

import com.google.common.base.Preconditions;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Resource;
import org.beetl.core.ResourceLoader;
import org.beetl.core.resource.StringTemplateResource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XmlTemplateResourceLoader implements ResourceLoader<String> {
    private String root;
    private final Map<String, String> rootMap = new HashMap();
    private final Map<Method, String> methodNameMappingNgqlMap = new HashMap();

    public XmlTemplateResourceLoader(String root) {
        this.root = root;
    }

    public Resource getResource(String key) {
        return new StringTemplateResource((String)this.rootMap.get(key), this);
    }

    public boolean isModified(Resource resource) {
        return false;
    }

    public boolean exist(String key) {
        return this.rootMap.containsKey(key);
    }

    public void close() {
    }

    public void init(GroupTemplate groupTemplate) {
        Element rootElement = getRootElement(this.root);
        Map<String, String> map = parseXmlElement(rootElement);
        String mapperPackageName = parseNamespace(rootElement);
        Class<?> aClass = null;

        try {
            aClass = Class.forName(mapperPackageName);
        } catch (ClassNotFoundException var13) {
            ClassNotFoundException e = var13;
            throw new RuntimeException(e);
        }

        Boolean isAnnotationPresent = aClass.isAnnotationPresent(GraphBatisMapper.class);
        Preconditions.checkArgument(isAnnotationPresent, String.format("mapper类 %s未添加@GraphBatisMapper注解", mapperPackageName));
        Method[] methods = aClass.getMethods();
        Method[] var8 = methods;
        int var9 = methods.length;

        for(int var10 = 0; var10 < var9; ++var10) {
            Method method = var8[var10];
            String methodName = method.getName();
            if (!map.containsKey(methodName)) {
                throw new RuntimeException(String.format("无效的方法名%s", methodName));
            }

            this.methodNameMappingNgqlMap.put(method, (String)map.get(methodName));
        }

        this.rootMap.putAll(map);
    }

    public String getResourceId(Resource resource, String key) {
        return "";
    }

    public String getInfo() {
        return "XmlTemplateResourceLoader";
    }

    private static Map<String, String> parseXmlElement(Element rootElement) {
        List<Element> queryElements = rootElement.elements("query");
        Map<String, String> map = new HashMap();
        Iterator var3 = queryElements.iterator();

        while(var3.hasNext()) {
            Element queryElement = (Element)var3.next();
            map.put(queryElement.attribute("id").getValue(), queryElement.getText());
        }

        return map;
    }

    private static String parseNamespace(Element rootElement) {
        return rootElement.attribute("namespace").getValue();
    }

    private static Element getRootElement(String xmlLocation) {
        SAXReader reader = new SAXReader();
        Document document = null;

        try {
            document = reader.read((new ClassPathResource(xmlLocation)).getInputStream());
        } catch (DocumentException var4) {
            DocumentException e = var4;
            throw new RuntimeException(e);
        } catch (IOException var5) {
            IOException e = var5;
            throw new RuntimeException(e);
        }

        return document.getRootElement();
    }
}
